package com.panshi.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.panshi.interceptor.GlobalInterceptor;
import com.panshi.tools.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;


public class CommonController extends Controller {
    public static Logger LOG = LoggerFactory.getLogger(IndexController.class);

    /**
     * 接口地址   /common/info
     * 授权  无
     * 请求参数
     */
    @ActionKey("/common-data")
    @Before(GlobalInterceptor.class)
    public void info() {

        String packageId = this.getHeader("Package-Id");
        System.out.printf(packageId);
        Record data = new Record();//数据封装内容

        //公共信息封装
        Record dataSet = new Record();//数据封装内容
        List<Record> commonInfos = Db.find("select *  from ot_app_common_info");
        for (Record commonInfo : commonInfos)
            dataSet.set(commonInfo.getStr("title"), commonInfo.getStr("value"));


        //上架控制开关
        List<Record> recordsPayStatus = Db.find("select *  from ot_app_pay_control where  packageId='" + packageId + "'");
        Boolean open = true;//支付开关  pay_status  0:关闭支付  1.开启支付
        if (recordsPayStatus.size() == 0)
            open = true;
        else
            open = false;
        data.set("open", open);


        //app名称
        data.set("appName", dataSet.getStr("app_real_name"));
        //app假名（上架用）
        data.set("appFakeName", dataSet.getStr("app_check_name"));


        //首页banner
        List<Record> indexBanner = Db.find("select image_url,link_url,sort,title from ot_app_banner  where `status`=1");
        data.set("indexBanner", indexBanner);


        //风控强平比率
        data.set("riskControlRate", dataSet.getStr("dangerlevel"));


        //首页底部说明
        String[] bottomSplit;
        List<Record> bottom_data = Db.find("select *  from ot_app_bottom_info where packageId='" + packageId + "'");
        if (
                bottom_data.size() > 0
                ) {
            String bottomvalue = bottom_data.get(0).get("value");
            bottomSplit = bottomvalue.split("----");
        } else {
            String bottomvalue = dataSet.get(getAttr("os") + "bottom");
            bottomSplit = bottomvalue.split("----");
        }
        data.set("indexBottom", bottomSplit);


        //充值提现汇率
        data.set("exchangeRate", dataSet.getStr("currency"));

        //提现说明
        String exvalue = dataSet.getStr("excomment");
        String[] withdrawComment = exvalue.split("----");
        data.set("withdrawComment", withdrawComment);


        //买价／卖价交替频率（毫秒）
        List<Record> records =
                Db.find("select  number  from ot_app_chart_control ORDER BY sort asc");
        String[] priceChangeRate = new String[records.size()];
        for (int i = 0; i < records.size(); i++) {
            priceChangeRate[i] = records.get(i).getStr("number");
        }
        data.set("priceChangeRate", priceChangeRate);

        renderJson(ResponseData.ok().putDataValue("commonData",data));
    }
}
